package com.special.ResideMenuDemo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

import com.special.ResideMenu.ResideMenu;
import com.special.ResideMenu.ResideMenuInfo;
import com.special.ResideMenu.ResideMenuItem;
import com.zsl.bean.OrderBean;
import com.zsl.dao.OrderDao;
import com.zsl.dao.impl.OrderDaoImpl;
import com.zsl.order.adapter.OrderAdapter;
import com.zsl.order.db.OrderDB;
import com.zsl.util.Util;

public class MainActivity extends FragmentActivity implements
		View.OnClickListener {

	private ResideMenu resideMenu;

	private ResideMenuItem itemHuiyuan;
	private ResideMenuItem itemQianbao;
	private ResideMenuItem itemZhuangban;
	private ResideMenuItem itemShoucang;
	private ResideMenuItem itemXiangce;
	private ResideMenuItem itemFile;
	private ResideMenuInfo info;
	private boolean is_closed = false;
	private long mExitTime;
	private Button leftMenu;

	// 控件
	private Spinner spinner1;
	private Spinner spinner2;

	private EditText editText_people;
	private TextView tv_money;
	private Button bt_add;
	private ListView lv_pro;
	private Button bt_ok;
	private TextView tv_order_money;
	

	/**
	 * Called when the activity is first created.
	 */
	// 分类
	String[] Category = { "主食", "盖浇饭(面)", "砂锅类", "素菜", "肉菜", "凉菜", "特色罐罐面",
			"汤类","饮料酒水" };
	//
	String[][] food = {
			{ "刀削面(小)--5元", "刀削面(大)--6元", "西红柿鸡蛋面--8元", "尖椒肉丝面--8元",
					"榨菜肉丝面--8元", "酸菜肉丝面--8元", "炸酱面--8元", "油泼面--8元", "麻辣凉面--8元",
					"鸡蛋面--8元", "羊杂烩面--10元", "肉丝炒面--9元", "土豆丝炒饼--8元", "肉炒饼--9元",
					"蛋炒饭--7元", "肉丝炒饭--8元", "火腿炒饭--8元", "扬州炒饭--8元", "猪肉水饺--10元",
					"韭菜鸡蛋水饺--9元"
			},
			{ "酸辣土豆丝盖饭(面)--8元","酸辣白菜盖饭(面)--8元","麻辣豆腐盖饭(面)--8元","西红柿鸡蛋盖饭(面)--9元",
						"尖椒鸡蛋盖饭(面)--9元","土豆肉片盖饭(面)--10元","鱼香肉丝盖饭(面)--10元","蒜蓉肉丝盖饭(面)--13元",
						"榨菜肉丝盖饭(面)--13元","茄子肉丝盖饭(面)--10元","回锅肉盖饭(面)--13元","过油肉盖饭(面)--13元",
						"木须肉盖饭(面)--13元","孜然肉片盖饭(面)--13元","木耳肉片盖饭(面)--13元","炝炒羊杂盖饭(面)--13元",
						"红烧肥肠盖饭(面)--16元"
			},
			{
				"砂锅土豆粉--9元","砂锅米线--9元","砂锅刀削面--9元","砂锅豆腐--10元",
				"牛肉砂锅--15元","砂锅两掺--9元","羊杂砂锅--15元","肥肠砂锅--15元","鸡块砂锅--15元",
				"排骨砂锅--15元"
			},
			{
				"尖椒土豆丝--9元","红烧豆腐--12元","麻辣豆腐--9元","家常豆腐--16元","红烧茄子--14元",
				"地三鲜--16元","干炒蘑菇--18元","干煸豆角--16元","烧腐竹--14元","尖椒炒鸡蛋--12元",
				"西红柿炒鸡蛋--12元","木耳炒鸡蛋--14元","摊鸡蛋--14元","炝炒圆白菜--14元","香菇青菜--10元",
				"烧油麦--12元","酸辣白菜--9元"
			},
			{
				"尖椒肉丝--18元","鱼香肉丝--18元","京酱肉丝--20元","香辣肉丝--18元","榨菜肉丝--18元",
				"蒜蓉肉丝--18元","木须肉--18元","回锅肉--18元","过油肉--18元","土豆肉片--18元",
				"宫保鸡丁--20元","辣子鸡丁--18元","芹菜炒肉--18元","糖醋里脊--25元","干炸里脊--25元",
				"香辣羊杂--18元","红烧鸡块--25元","孜然肉片--20元","大盘鸡--45元","红烧肥肠--25元",
				"红烧带鱼--25元","豆角炒肉--18元","香菇肉片--20元","干炸带鱼--25元","麻辣鸡丝--18元",
				"茄子鸡丝--18元","红烧排骨--30元","清炖排骨--30元","糖醋排骨--35元","猪肉炖粉条--25元"
				
			},
			{
				"素拼--7元","一人份花生米--2元","油炸花生米--10元","皮蛋豆腐--8元","老醋花生--10元",
				"姜汁松花蛋--9元","老虎菜--8元","凉拌猪头肉--18元"
			},
			{
				"火腿罐面--8元","羊杂罐面--10元","鸡块罐面--10元","排骨罐面--10元",
				"牛肉罐面--10元","素罐面--7元",
			},
			{
				"西红柿鸡蛋汤--6元","紫菜蛋花汤--6元","青菜豆腐汤--6元","疙瘩汤--8元",
				"西湖牛肉羹--15元","西施牛腩--40元"
			},
			{
				"饮料(大)--4元","饮料(小)--3元","啤酒--3元","大白--15元","小白--7元","口杯--5元"
			}
			
	};
	
	private Handler handler;
	List<Map<String, Object>> orderList=new ArrayList<Map<String, Object>>();
	public OrderAdapter adapter;
	
	
	int people;
	String foodName;
	int money;
	private Util util=new Util();
	//数据库
	OrderDB orderDB;
	private static OrderDao orderDao;
	private OrderBean orderBean=new OrderBean();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		orderDB=new OrderDB(this);
		orderDao=new OrderDaoImpl(this);
		setUpMenu();
		setListener();
		//handler
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("name", "菜品名称");
		map.put("people", "人数");
		map.put("money", "金额");
		Map<String, Object> map2=new HashMap<String, Object>();
		orderList.add(map);
		adapter=new OrderAdapter(this, orderList);
		lv_pro.setAdapter(adapter);
		adapter.notifyDataSetChanged();
		handler=new Handler(){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				switch (msg.what) {
				case 1:
				{
					int orderMoney=0;
					for (int i = 1; i < orderList.size(); i++) {
						String moneyString=""+orderList.get(i).get("money");
						int money=Integer.parseInt(moneyString);
						orderMoney+=money;
					}
					tv_order_money.setText(orderMoney+"");
					adapter.notifyDataSetChanged();
				}
				break;
				case 2:
				{
					Map<String, Object> map=new HashMap<String, Object>();
					map.put("name", "菜品名称");
					map.put("people", "人数");
					map.put("money", "金额");
					orderList.add(map);
					int orderMoney=0;
					for (int i = 1; i < orderList.size(); i++) {
						String moneyString=""+orderList.get(i).get("money");
						int money=Integer.parseInt(moneyString);
						orderMoney+=money;
					}
					tv_order_money.setText(orderMoney+"");
					adapter.notifyDataSetChanged();
				}
				break;
				
				default:
					break;
				}
			}
		};
	}

	@SuppressWarnings("deprecation")
	private void setUpMenu() {
		leftMenu = (Button) findViewById(R.id.title_bar_left_menu);
		lv_pro=(ListView) findViewById(R.id.main_lv_listView1);
		bt_ok=(Button) findViewById(R.id.main_bt_ok);
		tv_order_money=(TextView) findViewById(R.id.main_tv_order_money);
		// 控件
		spinner1 = (Spinner) findViewById(R.id.main_spinner1);
		spinner2 = (Spinner) findViewById(R.id.main_spinner2);
		editText_people = (EditText) findViewById(R.id.main_editText1);
		tv_money = (TextView) findViewById(R.id.main_textView_money);
		bt_add = (Button) findViewById(R.id.main_bt_add);
		ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(
				MainActivity.this, R.layout.spinner_item,
				R.id.spinner_item__tv_name, Category);
		spinner1.setAdapter(adapter1);
		spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(
						MainActivity.this, R.layout.spinner_item,
						R.id.spinner_item__tv_name, food[arg2]);
				spinner2.setAdapter(adapter2);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}

		});
		spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				String peopleString = editText_people.getText() + "";
				if (peopleString.equals("")) {
					Toast.makeText(MainActivity.this, "公主，快去输入人数吧，王子爱你，加油",
							Toast.LENGTH_SHORT).show();
					return;
				} else {
					try {
						people = Integer.parseInt(peopleString);
						foodName= spinner2.getSelectedItem() + "";
						int start = foodName.indexOf("--") + 2;
						int end = foodName.indexOf("元");
						@SuppressWarnings("unused")
						String priceString = foodName.substring(start, end);
						int price = Integer.parseInt(priceString);
						tv_money.setText("" + people * price);
					} catch (NumberFormatException e) {
						// TODO Auto-generated catch block
						tv_money.setText("");
						Toast.makeText(MainActivity.this, "公主，这里输入数字就可以了，么么哒",
								Toast.LENGTH_SHORT).show();
					}
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});

		editText_people.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub
				try {
					String peopleString = editText_people.getText() + "";
					people = Integer.parseInt(peopleString);
					foodName= spinner2.getSelectedItem() + "";
					int start = foodName.indexOf("--") + 2;
					int end = foodName.indexOf("元");
					@SuppressWarnings("unused")
					String priceString = foodName.substring(start, end);
					int price = Integer.parseInt(priceString);
					tv_money.setText("" + people * price);
					
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					tv_money.setText("");
					Toast.makeText(MainActivity.this, "公主，这里输入数字就可以了，么么哒",
							Toast.LENGTH_SHORT).show();
				}

			}
		});
		bt_add.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					foodName= spinner2.getSelectedItem() + "";
					String moneys=tv_money.getText()+"";
					money=Integer.parseInt(moneys);
					if (money!=0&&!"".equals(foodName)) {
						Map<String, Object> map=new HashMap<String, Object>();
						map.put("name",foodName);
						map.put("people", people+"");
						map.put("money", money+"");
						orderList.add(map);
						handler.sendEmptyMessage(1);
					}
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
		
		bt_ok.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String listMoney=tv_order_money.getText()+"";
				if (!"0".equals(listMoney)) {
					orderBean.setOrderName(util.getJson(orderList));
					orderBean.setMoney(listMoney);
					String time=util.getNowTime();
					orderBean.setTime(time);
					
					if (orderDao.save(orderBean)) {
						Toast.makeText(MainActivity.this, "公主，订单增加一笔喽，加油",
								Toast.LENGTH_SHORT).show();
						tv_order_money.setText("0");
						editText_people.setText("");
						tv_money.setText("0");
						orderList.removeAll(orderList);
						handler.sendEmptyMessage(2);
					}else{
						Toast.makeText(MainActivity.this, "公主，王子错了，添加失败了!_!",
								Toast.LENGTH_SHORT).show();
					}
				}else{
					Toast.makeText(MainActivity.this, "公主，要添加订单才可以OK的哦",
							Toast.LENGTH_SHORT).show();
				}
				
			}
		});
		lv_pro.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				orderList.remove(arg2);
				handler.sendEmptyMessage(1);
				return false;
			}
		});
		// attach to current activity;
		resideMenu = new ResideMenu(this);
		resideMenu.setBackground(R.drawable.menu_background);
		resideMenu.attachToActivity(this);
		resideMenu.setMenuListener(menuListener);
		// valid scale factor is between 0.0f and 1.0f. leftmenu'width is
		// 150dip.
		resideMenu.setScaleValue(0.6f);
		// 禁止使用右侧菜单
		resideMenu.setDirectionDisable(ResideMenu.DIRECTION_RIGHT);

		// create menu items;
		itemHuiyuan = new ResideMenuItem(this, R.drawable.ic_launcher, "今日订单");
		itemQianbao = new ResideMenuItem(this, R.drawable.ic_launcher, "输入查询");
		itemZhuangban = new ResideMenuItem(this, R.drawable.ic_launcher, "个性装扮");
		itemShoucang = new ResideMenuItem(this, R.drawable.ic_launcher, "我的收藏");
		itemXiangce = new ResideMenuItem(this, R.drawable.ic_launcher, "我的相册");
		itemFile = new ResideMenuItem(this, R.drawable.ic_launcher, "我的文件");

		resideMenu.addMenuItem(itemHuiyuan, ResideMenu.DIRECTION_LEFT);
		resideMenu.addMenuItem(itemQianbao, ResideMenu.DIRECTION_LEFT);
		resideMenu.addMenuItem(itemZhuangban, ResideMenu.DIRECTION_LEFT);
		resideMenu.addMenuItem(itemShoucang, ResideMenu.DIRECTION_LEFT);
		resideMenu.addMenuItem(itemXiangce, ResideMenu.DIRECTION_LEFT);
		resideMenu.addMenuItem(itemFile, ResideMenu.DIRECTION_LEFT);

		info = new ResideMenuInfo(this, R.drawable.icon_profile, "公主",
				"520 级");

	}

	private void setListener() {
		resideMenu.addMenuInfo(info);

		itemHuiyuan.setOnClickListener(this);
		itemQianbao.setOnClickListener(this);
		itemZhuangban.setOnClickListener(this);
		itemShoucang.setOnClickListener(this);
		itemXiangce.setOnClickListener(this);
		itemFile.setOnClickListener(this);

		info.setOnClickListener(this);

		leftMenu.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				resideMenu.openMenu(ResideMenu.DIRECTION_LEFT);
			}
		});
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		return resideMenu.dispatchTouchEvent(ev);
	}

	@Override
	public void onClick(View view) {
		if (view == itemHuiyuan) {
			Intent intent = new Intent();
			intent.putExtra("flog", "今日订单");
			intent.setClass(getApplicationContext(), TodayActivity.class);
			startActivity(intent);
		} else if (view == itemQianbao) {
			Intent intent = new Intent();
			intent.putExtra("flog", "钱包");
			intent.setClass(getApplicationContext(), TodayActivity.class);
			startActivity(intent);
		} else if (view == itemZhuangban) {
			Intent intent = new Intent();
			intent.putExtra("flog", "装扮");
			intent.setClass(getApplicationContext(), TodayActivity.class);
			startActivity(intent);
		} else if (view == itemShoucang) {
			Intent intent = new Intent();
			intent.putExtra("flog", "收藏");
			intent.setClass(getApplicationContext(), TodayActivity.class);
			startActivity(intent);
		} else if (view == itemXiangce) {
			Intent intent = new Intent();
			intent.putExtra("flog", "相册");
			intent.setClass(getApplicationContext(), TodayActivity.class);
			startActivity(intent);
		} else if (view == itemFile) {
			Intent intent = new Intent();
			intent.putExtra("flog", "文件");
			intent.setClass(getApplicationContext(), TodayActivity.class);
			startActivity(intent);
		} else if (view == info) {
			Intent intent = new Intent();
			intent.putExtra("flog", "个人信息");
			intent.setClass(getApplicationContext(), TodayActivity.class);
			startActivity(intent);
		}
	}

	private ResideMenu.OnMenuListener menuListener = new ResideMenu.OnMenuListener() {
		@Override
		public void openMenu() {
			is_closed = false;
			leftMenu.setVisibility(View.GONE);
		}

		@Override
		public void closeMenu() {
			is_closed = true;
			leftMenu.setVisibility(View.VISIBLE);
		}
	};

	// What good method is to access resideMenu？
	public ResideMenu getResideMenu() {
		return resideMenu;
	}

	// 监听手机上的BACK键
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// 判断菜单是否关闭
			if (is_closed) {
				// 判断两次点击的时间间隔（默认设置为2秒）
				if ((System.currentTimeMillis() - mExitTime) > 2000) {
					Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
					mExitTime = System.currentTimeMillis();
				} else {
					finish();
					System.exit(0);
					super.onBackPressed();
				}
			} else {
				resideMenu.closeMenu();
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	
	//获取当前时间
	public String getNowTime(){
		try {
//			SimpleDateFormat dateFormat=new SimpleDateFormat("yyy-MM-DD HH:mm:ss");
			SimpleDateFormat dateFormat=new SimpleDateFormat("yyy-MM-DD");
			String time=dateFormat.format(new Date());
			return time;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return "";
		}
	}
	
	public OrderDB getOrderDB() {
		return orderDB;
	}

	public void setOrderDB(OrderDB orderDB) {
		this.orderDB = orderDB;
	}

	public static OrderDao getOrderDao() {
		return orderDao;
	}

	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}
}
